import { _decorator, Component, Node, Label, Sprite, AudioClip, find, Layout, randomRange, director, v3 } from 'cc';
import { SSS_Consts } from "../../../../../../../Script/Data/SSS_Consts";
import { bdlMgr } from "../../../../../../../Script/Manager/BundleManager_S";
import UI from "../../../../../../../Script/Manager/UI";
import { ChengYu_GameData } from "../Data/ChengYu_GameData";
import ChengYu_Main from '../Manager/ChengYu_Main';
import ChengYu_IdiomInfoPanel from './ChengYu_IdiomInfoPanel';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

@ccclass('ChengYu_WinPanel')
export default class ChengYu_WinPanel extends Component {
    public static Instance: ChengYu_WinPanel;

    @property(Node)
    Node_idiom: Node | null = null;
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_next: Node | null = null;

    @property(Label)
    Label_level: Label | null = null;
   
    @property(Label)
    Label_time: Label | null = null;
    @property([Node])
    nodeList: Node[] = [];
    @property([Node])
    rowList: Node[] = [];
  

    data: any;

    onLoad() {
        ChengYu_WinPanel.Instance = this;

        this.node.active = false;

        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_next.on(Node.EventType.TOUCH_END, this.On_Button_next, this);
    }

    ShowOn(params?: any) {
        this.node.active = true;

        this.data = params;

        this.Init();

        let audio_root = `audio/Win sound/Win sound 4`;
        console.log(`audio_root`, audio_root);
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });
    }

    Init() {
        for (let index = 0; index < this.nodeList.length; index++) {
            const node = this.nodeList[index];
            node.active = false;
        }
        for (let index = 0; index < this.rowList.length; index++) {
            const node = this.rowList[index];
            node.active = false;
        }

        let dataShow = [
            { count: 1, nodeList: [0], rowList: [0] },
            { count: 2, nodeList: [0, 1], rowList: [0] },
            { count: 3, nodeList: [0, 1, 2], rowList: [0] },
            { count: 4, nodeList: [0, 1, 3, 4], rowList: [0, 1] },
            { count: 5, nodeList: [0, 1, 3, 4, 5], rowList: [0, 1] },
            { count: 6, nodeList: [0, 1, 2, 3, 4, 5], rowList: [0, 1] },
        ];

        const count: number = this.data.length;

        let arr = dataShow.filter((item) => {
            return item.count == count;
        });
        let item1 = arr[0];

        for (let index = 0; index < item1.nodeList.length; index++) {
            const index_nodeList = item1.nodeList[index];
            const Node_chengyu = this.nodeList[index_nodeList];
            Node_chengyu.active = true;

            if (count === 1) {
                Node_chengyu.scale = v3(1.3, 1.3, 1.3) // 1.3;
            } else if (count === 2) {
                Node_chengyu.scale = v3(1.1, 1.1, 1.1) //1.1;
            } else {
                Node_chengyu.scale = v3(1, 1, 1) // 1;
            }

            const chengyu = this.data[index];

            const Label_idiom: Label = find("Label_idiom", Node_chengyu).getComponent(Label);
            Label_idiom.string = chengyu;

            Node_chengyu.on(Node.EventType.TOUCH_START, () => {
                UI.Instance.CreatePanel_sub(`ChengYu_Prefab`, `ChengYu_IdiomInfoPanel`, ChengYu_IdiomInfoPanel, chengyu);
            }, this);
        }

        for (let index = 0; index < item1.rowList.length; index++) {
            const index_rowList = item1.rowList[index];
            const node = this.rowList[index_rowList];
            node.active = true;
        }

        for (let index = 0; index < this.rowList.length; index++) {
            let count = 0;

            const row = this.rowList[index];
            for (let index = 0; index < row.children.length; index++) {
                const node = row.children[index];
                if (node.active === true) {
                    count++;
                }
            }

            const layout: Layout = row.getComponent(Layout);
            if (count === 2) {
                layout.spacingX = 50;
            } else {
                layout.spacingX = 10;
            }
        }

        let level = ChengYu_GameData.Get_CurrentLevelIndex();

        this.Label_level.string = `第${level}关`;

        let numberBei: number = 10;
        let n1 = Math.floor(level / numberBei);

        if (level % numberBei === 0) {
            n1--;
        }

        // 超过80.0 - 98.0%用户
        const minute = (100 + Math.floor(ChengYu_GameData.GameData.GamePlayTime / 60)).toString().slice(1);
        const second = (100 + (ChengYu_GameData.GameData.GamePlayTime % 60)).toString().slice(1);
        let randomUser = randomRange(60, 98);
        randomUser = Math.floor(randomUser * 100) / 100;
        this.Label_time.string = `时长 ${minute}:${second} 超过${randomUser}%用户`;
    }

    On_Button_back() {
        director.loadScene(ChengYu_GameData.SCENE_NAME.main);
    }

    On_Button_next() {
        ChengYu_Main.Instance.下一关();
    }
}

/** 调试 */
window["ChengYu_WinPanel"] = ChengYu_WinPanel;